package com.javandad.app.shared.entity.project;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;


@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "false")
public class ProjectAssoc extends com.javandad.app.shared.entity.Notification implements Serializable {

	private static final long serialVersionUID = 4889600651004902732L;
	
	public final static int NOASSOC = 1;
	public final static int FOLLOWING_PROJECT = 2;
	public final static int PARTICIPATING_PROJECT = 4;
	public final static int OWNING_PROJECT = 8;
	
	
	public ProjectAssoc(String project, String user, int level, Date dateBegin){
		this.username = user;
		this.projectname = project;
		this.dateBegin = dateBegin;
		this.setLevel(level);
	}
	@SuppressWarnings("unused")
	private ProjectAssoc() {}
	
	public String getProjectname() {
		return projectname;
	}

	public void setProjectname(String projectname) {
		this.projectname = projectname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public boolean isOwner() {
		return isLevel(OWNING_PROJECT);
	}
	public boolean isParticipating() {
		return isLevel(PARTICIPATING_PROJECT);
	}
	public boolean isFollowing() {
		return isLevel(FOLLOWING_PROJECT);
	}
	private boolean isLevel(int bitmask) {
		return (level & bitmask) != 0;
	}
	public void setOwner(boolean owner) {
		setLevel(OWNING_PROJECT, owner);
	}
	public void setParticipating(boolean part) {
		setLevel(PARTICIPATING_PROJECT, part);
	}
	public void setFollowing(boolean follow) {
		setLevel(FOLLOWING_PROJECT, follow);
	}
	private void setLevel(int bitmask, boolean value) {
		level = (level & ~bitmask) | (value ? bitmask : 0);
	}

	public long getId() {
		return id;
	}		
	
	public Date getDateBegin() {
		return dateBegin;
	}
	public void setDateBegin(Date dateBegin) {
		this.dateBegin = dateBegin;
	}

	public String getProjectFodoURL() {
		return projectFodoURL;
	}
	public void setProjectFodoURL(String projectFodoURL) {
		this.projectFodoURL = projectFodoURL;
	}

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	
	@Persistent
	private String projectname;
	
	@Persistent
	private String username;
	
	@Persistent
	private int level = FOLLOWING_PROJECT;
	
	@Persistent
	private Date dateBegin = null;
	
	private String projectFodoURL = "/img/icons/project_default_pic.png";

}
